package cn.itsy.web.controller;


import cn.itsy.dao.Menudao;
import cn.itsy.pojo.SysFunction;
import cn.itsy.pojo.SysRole;
import cn.itsy.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("menu")
public class MenuController {
    @Resource
    private Menudao dao;

    @RequestMapping("menulist")
    public List<SysFunction> list(HttpServletRequest request, String rid) {
        System.out.println(rid);
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String sql = "select id,function_name,pid from sys_function";
        String sql1 = "select f.id,f.function_name,f.pid from user u,sys_user_role ur,sys_role r,sys_role_function rf,sys_function f" +
                " where u.id = ur.user_id and r.id = ur.role_id and r.id = rf.role_id and f.id=rf.function_id and u.id='" + user.getId() + "'";
        if (rid != null && !rid.equals("")) {
            sql1 = "select f.id,f.function_name,f.pid from user u,sys_user_role ur,sys_role r,sys_role_function rf,sys_function f" +
                    " where u.id = ur.user_id and r.id = ur.role_id and r.id = rf.role_id and f.id=rf.function_id and r.id='" + rid + "'";
        }
        List<SysFunction> f = dao.role(sql1);
        List<SysFunction> menulist = dao.menulist(sql);
        ArrayList list = new ArrayList();
        for (SysFunction s : f) {
            list.add(s.getId());
        }
        for (int i = 0; i < menulist.size(); i++) {
            menulist.get(i).setChecked(list.contains(menulist.get(i).getId()));
        }
        return menulist;
    }

    @RequestMapping("rolelist")
    public List<Map<String, Object>> rolelist() {
        String sql = "select * from sys_role";
        List<Map<String, Object>> rolelist = dao.rolelist(sql);
        return rolelist;
    }

    @RequestMapping("menu")
    public String menu(String[] nodess, String rid) {
        Integer[] id = new Integer[nodess.length];
        int sub = 0;
        if (rid != null && !rid.equals("")) {
            String sql = "delete from sys_role_function where role_id='" + rid + "'";
            dao.delete(sql);
            for (String i : nodess) {
                if (i.charAt(0) == '[') {
                    i = i.replaceAll("\\[", "");
                }
                if (i.charAt(i.length() - 1) == ']') {
                    i = i.replaceAll("]", "");
                }
                if (!"".equals(i) && i != null) {
                    i = i.substring(1, i.length() - 1);
                    id[sub] = new Integer(i);
                    sub++;
                }
            }
            for (int i = 0; i < id.length; i++) {
                String sql1 = "insert into sys_role_function values (null,'" + rid + "','" + id[i] + "','Y')";
                dao.add(sql1);
            }
            return "修改功能点成功";
        } else {
            return "没有选择角色";
        }
    }
}
